<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="操作名称" prop="operation">
      <el-input v-model="dataForm.operation" placeholder="操作名称"></el-input>
    </el-form-item>
    <el-form-item label="操作类型" prop="type">
      <el-input v-model="dataForm.type" placeholder="操作类型"></el-input>
    </el-form-item>
    <el-form-item label="ip地址" prop="ip">
      <el-input v-model="dataForm.ip" placeholder="ip地址"></el-input>
    </el-form-item>
    <el-form-item label="操作人" prop="user">
      <el-input v-model="dataForm.user" placeholder="操作人"></el-input>
    </el-form-item>
    <el-form-item label="操作时间" prop="time">
      <el-input v-model="dataForm.time" placeholder="操作时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          operation: '',
          type: '',
          ip: '',
          user: '',
          time: ''
        },
        dataRule: {
          operation: [
            { required: true, message: '操作名称不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '操作类型不能为空', trigger: 'blur' }
          ],
          ip: [
            { required: true, message: 'ip地址不能为空', trigger: 'blur' }
          ],
          user: [
            { required: true, message: '操作人不能为空', trigger: 'blur' }
          ],
          time: [
            { required: true, message: '操作时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$request({
              url: `/logs/info/${this.dataForm.id}`,
              method: 'get',
              params: {}
            }).then(res => {
              if (res && res.code === '200') {
                this.dataForm.operation = res.data.operation
                this.dataForm.type = res.data.type
                this.dataForm.ip = res.data.ip
                this.dataForm.user = res.data.user
                this.dataForm.time = res.data.time
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$request({
              url: `/logs/${!this.dataForm.id ? 'save' : 'update'}`,
              method: 'post',
              data: {
                'id': this.dataForm.id || undefined,
                'operation': this.dataForm.operation,
                'type': this.dataForm.type,
                'ip': this.dataForm.ip,
                'user': this.dataForm.user,
                'time': this.dataForm.time
              }
            }).then(res => {
              if (res && res.code === '200') {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(res.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
